package com.hxtec.polaris.service.impl;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.hxtec.polaris.commons.api.vo.Result;
import com.hxtec.polaris.commons.constant.*;
import com.hxtec.polaris.commons.exception.MyException;
import com.hxtec.polaris.commons.thread.HistoryRunable;
import com.hxtec.polaris.entity.ShopHistory;
import com.hxtec.polaris.mapper.ShopHistoryMapper;
import com.hxtec.polaris.service.HistoryService;
import com.hxtec.polaris.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadFactory;

/**
 * @Describtion 历史记录相关service
 * @Author yonyong
 * @Date 2019/11/25 16:45
 * @Version 1.0.0
 **/
@Service
@Transactional
public class HistoryServiceImpl implements HistoryService{

    @Resource
    private ShopHistoryMapper shopHistoryMapper;
    @Resource
    private UserService userService;

    @Override
    public Object getAll() {
        try {
            List<Map<String,Object>> mapList = shopHistoryMapper.getAllHistory(userService.getUserInfo().getId());
            return Result.ok(mapList);
        }catch (Exception e){
            String LOG_MSG = MessageFormat.format(Log.PATTERN_BASE,userService.getUserInfo().toString());
            throw new MyException(Result.error(Code.FAIL_4401,Msg.HISTORY_GET_FAIL),LOG_MSG);
        }
    }

    @Override
    public Object insert(HttpServletRequest request, ShopHistory shopHistory) {
        //调用线程，更新path
        //把当前编辑的节点及其所有子节点挂到新的节点下面，更新路径
        // 创建线程池
        shopHistory.setUsername(String.valueOf(userService.getUserInfo().getId()));
        ThreadFactory pool = new ThreadFactoryBuilder().setNameFormat(GlobalVar.THREAD_HISTORY_INSERT).build();
        ServletContext context =  request.getServletContext();
        // 计数器
        CountDownLatch doneSignal = new CountDownLatch(1);
        pool.newThread(new HistoryRunable(context,doneSignal,shopHistory)).start();
        return Result.ok();
    }

    @Override
    public Object delete(ShopHistory shopHistory) {
        try {
            int result = shopHistoryMapper.delete(shopHistory.getId());
            if (1 != result){
                String LOG_MSG = MessageFormat.format(Log.PATTERN_BASE,userService.getUserInfo().toString());
                throw new MyException(Result.error(Code.FAIL_4401,Msg.HISTORY_DELETE_NULL),LOG_MSG);
            }
            return Result.ok();
        }catch (Exception e){
            String LOG_MSG = MessageFormat.format(Log.PATTERN_BASE,userService.getUserInfo().toString());
            throw new MyException(Result.error(Code.FAIL_4401,Msg.HISTORY_DELETE_FAIL),LOG_MSG);
        }
    }

    @Override
    public Object deleteAll() {
        try {
            int result = shopHistoryMapper.deleteAll(String.valueOf(userService.getUserInfo().getId()));
            if (1 > result){
                String LOG_MSG = MessageFormat.format(Log.PATTERN_BASE,userService.getUserInfo().toString());
                throw new MyException(Result.error(Code.FAIL_4401, Msg.HISTORY_TRUNK_NULL),LOG_MSG);
            }
            return Result.ok();
        }catch (Exception e){
            String LOG_MSG = MessageFormat.format(Log.PATTERN_BASE,userService.getUserInfo().toString());
            throw new MyException(Result.error(Code.FAIL_4401,Msg.HISTORY_TRUNK_FAIL),LOG_MSG);
        }
    }
}
